module(..., package.seeall)

local function test_sum(ad,id,val)
	return (ad or 0)+1
end

function sum_calc(db,rootid,fun)	
	return db:get_nodes_sum(rootid,fun or test_sum)
end


function label_inc(db,rid,timestamp,usr,fun)
	local idx,sum,ts,ul = db:get_label(rid)
	idx=idx+1
	timestamp=timestamp or os.date("!%Y-%m-%d %H:%M:%S")
	local nsum=tostring(sum_calc(db,rid,fun))
	db:set_label(rid,idx,nsum,timestamp,usr)
	return tostring(idx) .. '|' .. nsum .. '|' .. timestamp .. '|' .. (usr or '')
end

function label_string(db,id)
	local idx,sum,ts,ul = db:get_label(id)
	return tostring(idx) .. '|' .. sum .. '|' .. ts .. '|' .. (ul or '')
end